package com.eight.vote.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eight.vote.pojo.VMessage;
import com.eight.vote.pojo.VOption;
import com.eight.vote.pojo.VOptionInfo;
import com.eight.vote.mapper.VOptionInfoMapper;
import com.eight.vote.service.VMessageService;
import com.eight.vote.service.VOptionInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eight.vote.service.VOptionService;
import com.eight.vote.util.Result;
import com.eight.vote.util.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 点点星言
 * @since 2022-10-16
 */
@Service
@Slf4j
public class VOptionInfoServiceImpl extends ServiceImpl<VOptionInfoMapper, VOptionInfo> implements VOptionInfoService {

        @Autowired
        VOptionService optionService;
        @Autowired
        VMessageService messageService;

    @Override
    public Result addOption(VOptionInfo optionInfo) {
        //补全信息
        Long userId = UserHolder.getUserId();
        Long optionId = optionInfo.getOptionId();
        LocalDateTime now = LocalDateTime.now();
        optionInfo.setDate(now);
        optionInfo.setUserId(userId);
        //数据校验
        //构建查询对象
        QueryWrapper<VOptionInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("u_id",userId).eq("o_id",optionId);
        //查询。根据结果判断
        VOptionInfo info = baseMapper.selectOne(wrapper);

        if (info!=null) {
            //如果存在，报不能重复投同一选项异常
            return Result.fail("不能重复投同一个选项");
        }
        //如果不存在，检索该选项在选项表中是否存在
        VOption option = optionService.getById(optionId);
        if (option == null) {
            //如果不存在，报该选项不存在异常
            return Result.fail("该选项不存在，不能进行投票");
        }
        //检索该选项对应的投票信息类型，
        Long msgId = option.getInfoId();
        VMessage message = messageService.getById(msgId);
        if (message.getStatus() == 2) {
            return Result.fail("投票已结束，请下次准时点！");
        }

        if (message.getType() == 1){
            baseMapper.insert(optionInfo);
        }
        //如果不是多选，则检索该用户是否已经投过该投票信息的选项，已经投过，直接报错
        QueryWrapper<VOption> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("i_id",msgId);
        List<VOption> options = optionService.getByWrapperList(queryWrapper);
        List<VOptionInfo> vOptionInfoList =  new ArrayList<VOptionInfo>();
        options.stream().forEach(item->{
            QueryWrapper<VOptionInfo> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("o_id",item.getOId());
            List<VOptionInfo> vOptionInfos = baseMapper.selectList(wrapper1);
            vOptionInfoList.addAll(vOptionInfos);
        });
        log.debug("在这里哦{}",vOptionInfoList.toString());

        for (VOptionInfo vOptionInfo : vOptionInfoList) {
            if (vOptionInfo.getUserId() == userId) {
                return Result.fail("单选不能重复选择");
            }
        }
        //检索uid和oid同时符合的数据，如果有这种数据，证明该用户之前之前已经投过该选项一次
            baseMapper.insert(optionInfo);
        //填表入库
        return Result.ok();
    }
}
